package ken.spring.cloud.alibaba;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.BlockResponse;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * <ul>
 * <li>Title: ZuulConfiguration</li>
 * <li>Description: TODO </li>
 * </ul>
 *
 * @author
 * @date 2022/6/29 22:15
 */
@Configuration
public class ZuulConfiguration {

    @Bean
    public ZuulBlockFallbackProvider zuulBlockFallbackProvider1() {
        return new ZuulBlockFallbackProvider() {
            @Override
            public String getRoute() {
                return "*";
            }

            @Override
            public BlockResponse fallbackResponse(String route, Throwable cause) {
                if ("my-service3".equals(route)) {
                    return new BlockResponse(433, "Sentinel Block3", route);
                }
                else if ("my-service4".equals(route)) {
                    return new BlockResponse(444, "my-service4", route);
                }
                else {
                    return new BlockResponse(499, "Sentinel Block 499", route);
                }
            }
        };
    }

    @Bean
    public RequestOriginParser requestOriginParser() {
        return new RequestOriginParser() {

            @Override
            public String parseOrigin(HttpServletRequest request) {
                return "123";
            }
        };
    }
}
